<?php
namespace app\manage\controller;

use app\applet\model\ElectricityMeter;
use app\applet\model\WaterMeter;
use app\applet\model\GasMeter;
use app\manage\model\AdminUser;
use think\Controller;
use think\Request;
use think\Validate;
use app\manage\model\ApartmentSettings;

class EstateManagement extends Controller {
    public function __construct(Request $request = null)
    {
//        parent::__construct($request);
    }

    /**
     * 抄表管理
     */
    public function MeterList() {
        $input = input();
        $validate = new Validate([
//            ['type', 'require', '不能为空'],
            ['admin_id', 'require', '不能为空'],
            ['page', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $status = input()['status'];//抄表状态
//        $type = input()['type'];//抄表类型
        $admin_id = input()['admin_id'];
        $page = input()['page'];
        $tung = input()['tung_num'];
        $unit = input()['unit_num'];
        $room_number = input()['room_number'];
        $time = input()['time'];// 1表示本月，2表示上月

        $model = new AdminUser();
        $admin_id = $model->findPlatId($admin_id);

//        switch ($type) {
//            case 1:
//                $this->ElectricityMeterList($admin_id, $status, $keyword, $page);
//                break;
//            case 2:
//                $this->WaterMeterList($admin_id, $status, $keyword, $page);
//                break;
//            case 3:
//                $this->GasMeterList($admin_id, $status, $keyword, $page);
//                break;
//            default:
//        }
        $electricity_data = $this->ElectricityMeterList($admin_id, $status, $page, $tung, $unit, $room_number, $time);
        $water_data = $this->WaterMeterList($admin_id, $status, $page, $tung, $unit, $room_number, $time);
        $gas_data = $this->GasMeterList($admin_id, $status, $page, $tung, $unit, $room_number, $time);

        // 组装水费、气费数据
        foreach ($electricity_data['data'] as $k => &$data) {
            $data['electricity_num'] = $data['num'];
            $data['electricity_image'] = $data['image'];
            unset($data['num']);
            unset($data['image']);

            $data['water_num'] = $water_data['data'][$k]['num'];
            $data['gas_num'] = $gas_data['data'][$k]['num'];
            $data['water_image'] = $water_data['data'][$k]['image'];
            $data['gas_image'] = $gas_data['data'][$k]['image'];
        }

        /**
         * 抄表时间
         */

        $model_electricity = new ElectricityMeter();
        $electricity_data['time'] = date('Y-m', time());
        $electricity_data['todo'] = $model_electricity->getTodo($admin_id);

        returnJson(200,"成功", $electricity_data)->send();die;
    }

    /**
     * 电费抄表管理
     * @param $admin_id
     * @param $status
     * @param $keyword
     * @param $page
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function ElectricityMeterList($admin_id, $status, $page, $tung, $unit, $room_number, $time) {
        $model = new ElectricityMeter();
        $data = $model->getIndex($admin_id, $page, get_list_rows(), $status, $tung, $unit, $room_number, $time);
        return $data;
    }

    /**
     * 水费抄表管理
     * @param $admin_id
     * @param $status
     * @param $keyword
     * @param $page
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function WaterMeterList($admin_id, $status, $page, $tung, $unit, $room_number, $time) {
        $model = new WaterMeter();
        $data = $model->getIndex($admin_id, $page, get_list_rows(), $status, $tung, $unit, $room_number, $time);
        return $data;
    }

    /**
     * 气费抄表管理
     * @param $admin_id
     * @param $status
     * @param $keyword
     * @param $page
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function GasMeterList($admin_id, $status, $page, $tung, $unit, $room_number, $time) {
        $model = new GasMeter();
        $data = $model->getIndex($admin_id, $page, get_list_rows(), $status, $tung, $unit, $room_number, $time);
        return $data;
    }

    /**
     * 抄表
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function EditMeterList() {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '不能为空'],
            ['electricity_num', 'require', '不能为空'],
//            ['electricity_image', 'require', '不能为空'],
            ['water_num', 'require', '不能为空'],
//            ['water_image', 'require', '不能为空'],
            ['gas_num', 'require', '不能为空'],
//            ['gas_image', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $data['id'] = input()['id'];
        $data_e['num'] = $input['electricity_num'];
        $data_e['image'] = implode(',', $input['electricity_image']);
        $data_w['num'] = $input['water_num'];
        $data_w['image'] = implode(',', $input['water_image']);
        $data_g['num'] = $input['gas_num'];
        $data_g['image'] = implode(',', $input['gas_image']);
        $data_e['remark'] = isset($input['remark']) ? $input['remark'] : '';
        $token = input()['token'];

        // 抄表日期在规定有效期内才称之为已抄表
        $model = new ApartmentSettings();
        $model_a = new AdminUser();
        $create_id = get_user_id($token);
        $create_id = $model_a->findPlatId($create_id);
        $setting = $model->index2($create_id);
        // 电费状态
        if ($data_e['num'] == '') {
            $data_e['status'] = 1;
        } else {
            if (date('d') >= $setting['inspection_time'] && date('d') <= $setting['end_inspection']) {
                $data_e['status'] = 2;
            } else {
                $data_e['status'] = 3;
            }
        }
        // 水费状态
        if ($data_w['num'] == '') {
            $data_w['status'] = 1;
        } else {
            if (date('d') >= $setting['inspection_time'] && date('d') <= $setting['end_inspection']) {
                $data_w['status'] = 2;
            } else {
                $data_w['status'] = 3;
            }
        }
        // 气费状态
        if ($data_g['num'] == '') {
            $data_g['status'] = 1;
        } else {
            if (date('d') >= $setting['inspection_time'] && date('d') <= $setting['end_inspection']) {
                $data_g['status'] = 2;
            } else {
                $data_g['status'] = 3;
            }
        }

        $model_e = new ElectricityMeter();
        $model_w = new WaterMeter();
        $model_g = new GasMeter();

        $model_e->m_update($data['id'], $data_e);
        $model_w->m_update($data['id'], $data_w);
        $model_g->m_update($data['id'], $data_g);

        returnJson(200,"成功")->send();

    }

    /**
     * 详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function DetailMeterList() {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $id = input()['id'];
        $page = input()['page'];

        $model_e = new ElectricityMeter();
        $data_e = $model_e->getRoomList($id, $page);
        $model_w = new WaterMeter();
        $data_w = $model_w->getRoomList($id, $page);
        $model_g = new GasMeter();
        $data_g = $model_g->getRoomList($id, $page);

        foreach ($data_e['data'] as $k => &$value) {
            $value['electricity_num'] = $value['num'];
            $value['electricity_image'] = $value['image'];
            unset($value['num']);
            unset($value['image']);

            $value['water_num'] = $data_w['data'][$k]['num'];
            $value['water_image'] = $data_w['data'][$k]['image'];
            $value['gas_num'] = $data_g['data'][$k]['num'];
            $value['gas_image'] = $data_g['data'][$k]['image'];
        }

        returnJson(200,"成功", $data_e)->send();

    }

    /**
     * 编辑时获取详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function EditDetail() {
        $id = input()['id'];
        $model_e = new ElectricityMeter();
        $model_w = new WaterMeter();
        $model_g = new GasMeter();

        $detail_e = $model_e->detail($id);
        $detail_w = $model_w->detail($id);
        $detail_g = $model_g->detail($id);

        $data['room_number'] = $detail_e['tung_num'] . '-' . $detail_e['unit_num'] . '-' . $detail_e['room_number'];
        $data['electricity_num'] = $detail_e['num'];
        $data['electricity_image'] = $detail_e['image'];
        $data['water_num'] = $detail_w['num'];
        $data['water_image'] = $detail_w['image'];
        $data['gas_num'] = $detail_g['num'];
        $data['gas_image'] = $detail_g['image'];
        $data['remark'] = $detail_e['remark'];

        returnJson(200,"成功", $data)->send();
    }

    /**
     * 额外信息
     */
    public function extraMeter() {
        $admin_id = input()['admin_id'];

        $data['time'] = date('Y年m月');
        $model = new ApartmentSettings();
        $data['meter_start_time'] = $model->index()['inspection_time'];
        $data['meter_end_time'] = $model->index()['end_inspection'];

        $model = new AdminUser();
        $admin_id = $model->findPlatId($admin_id);

        $model = new ElectricityMeter();
        $data['todo'] = $model->getTodo($admin_id);
        returnJson(200,"成功", $data)->send();
    }
}